package com.saunter.service;

import com.github.pagehelper.PageHelper;
import com.saunter.extendMapper.SalaryKeyExtendMapper;
import com.saunter.mapper.SalaryKeyMapper;
import com.saunter.model.Salary;
import com.saunter.model.SalaryKey;
import com.saunter.utils.Util;
import com.saunter.utils.excel.ExcelData;
import com.saunter.utils.excel.ExportExcelUtils;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.CustomPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service("SalaryKeyService")
public class SalaryKeyService {
    @Autowired
    private SalaryKeyExtendMapper salaryKeyExtendMapper;
    @Resource
    private ExceptionManager exceptionManager;

    public int add(SalaryKey salary_key) {
        if(null != this.checkExist(salary_key.getIdKey(), salary_key.getLabel())) {
            throw exceptionManager.create("SalK001");
        }
        return salaryKeyExtendMapper.insertSelective(salary_key);
    }

    public CustomPage<SalaryKey> getAll(int page_no, int page_size, Map<String, Object>filters) {
        PageHelper.startPage(page_no, page_size);
        return new CustomPage<SalaryKey>(salaryKeyExtendMapper.getAll(filters));
    }

    public int edit(SalaryKey new_salary_key) {
        if(null == this.checkExist(new_salary_key.getIdKey(),null)) {
            throw exceptionManager.create("SalK002");
        }
        String label = new_salary_key.getLabel();
        if(null != label && null != this.checkExist(null, label)) {
            throw exceptionManager.create("SalK001");
        }
        return salaryKeyExtendMapper.updateByPrimaryKeySelective(new_salary_key);
    }

    /**
     * 检查salary_key是否存在，主要在添加salary_key时使用
     * @param id_key
     * @param label
     * @return
     */
    public SalaryKey checkExist(String id_key, String label) {
        Map<String, String> filters = new HashMap<String, String>();
        filters.put("id_key", id_key);
        filters.put("label", label);
        return salaryKeyExtendMapper.getByKeyAndLabel(filters);
    }

    /**
     * 完整的查出所有启用的salary_key
     * @return
     */
    public List<SalaryKey> getAllOn() {
        return salaryKeyExtendMapper.getAllOn();
    }

    /**
     * 以字符串数组的形式返回所有启用的salary_key，可以用于简单的检测
     * 与getAllOn有一样的排序
     * @return
     */
    public List<String> getAllIdKeysOn() {
        return salaryKeyExtendMapper.getAllIdKeysOn();
    }

    public List<String> getAllLableOn() {
        return salaryKeyExtendMapper.getAllLableOn();
    }

    public List<String> getAllIdKeysShow() {
        return salaryKeyExtendMapper.getAllIdKeysShow();
    }
    public List<SalaryKey> getAllShow() {
        return salaryKeyExtendMapper.getAllShow();
    }
    /**
     * 检查传入的salary_key是否和数据库中开启的key一样
     * @param id_keys
     * @return
     */
    public Boolean checkIdKeys(List<String> id_keys) {
        return Util.equalList(id_keys, this.getAllIdKeysOn());
    }

    public Boolean checkLables(List<String> lables) {
        return Util.equalList(lables, this.getAllLableOn());
    }



    /**
     * 下载Excel模板
     * @param response
     * @throws Exception
     */
    public int getExcelTemplate(HttpServletResponse response) throws Exception {
        ExcelData excelData = new ExcelData();
        excelData.setName("record");

        List<String> titles = new ArrayList<String>(){{add("序号");add("工号");}};
        titles.addAll(this.getAllLableOn());

        excelData.setTitles(titles);
        ExportExcelUtils.exportExcel(response, "record_template.xlsx",excelData);
        return 1;
    }
}
